/**
 * 阅读计划相关API接口
 */

import type {
  PlanDailyTaskRespVO,
  ReadingPlanCreateReqVO,
  ReadingPlanDetailRespVO,
  ReadingPlanRespVO,
} from './types/plan'
import { http } from '@/utils/request'

/**
 * 创建阅读计划
 */
export function createReadingPlan(data: ReadingPlanCreateReqVO) {
  return http.post<number>('/reading/reading-plans/create', data)
}

/**
 * 获取用户阅读计划列表
 */
export function getReadingPlanList(status?: string) {
  return http.get<ReadingPlanRespVO[]>('/reading/reading-plans/list', {
    status,
  })
}

/**
 * 获取阅读计划详情
 */
export function getReadingPlanDetail(id: number) {
  return http.get<ReadingPlanDetailRespVO>(`/reading/reading-plans/${id}`)
}

/**
 * 删除阅读计划
 */
export function deleteReadingPlan(id: number) {
  return http.delete<boolean>(`/reading/reading-plans/${id}`)
}

/**
 * 获取计划任务列表
 */
export function getPlanTasks(planId: number, params?: { day?: number, status?: string }) {
  return http.get<PlanDailyTaskRespVO[]>(`/reading/reading-plans/${planId}/tasks`, params)
}

/**
 * 完成任务
 */
export function completeTask(taskId: number, readingTime?: number, notes?: string) {
  const queryParams: string[] = []
  if (readingTime !== undefined)
    queryParams.push(`readingTime=${readingTime}`)
  if (notes)
    queryParams.push(`notes=${encodeURIComponent(notes)}`)

  const queryString = queryParams.join('&')
  const url = `/reading/reading-plans/tasks/${taskId}/complete${queryString ? `?${queryString}` : ''}`

  return http.post<boolean>(url)
}

/**
 * 跳过任务
 */
export function skipTask(taskId: number, reason?: string) {
  const queryParams: string[] = []
  if (reason)
    queryParams.push(`reason=${encodeURIComponent(reason)}`)

  const queryString = queryParams.join('&')
  const url = `/reading/reading-plans/tasks/${taskId}/skip${queryString ? `?${queryString}` : ''}`

  return http.post<boolean>(url)
}

/**
 * 重置任务
 */
export function resetTask(taskId: number) {
  return http.post<boolean>(`/reading/reading-plans/tasks/${taskId}/reset`)
}
